/**
 * 
 */
package com.huarui.desktop;


import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Properties;

import net.sf.json.JSONObject;

import org.apache.struts2.ServletActionContext;
import org.ewaf.framework.action.BaseAction;

import com.huarui.desktop.model.*;
import com.huarui.desktop.service.ModuleFactory;
import com.opensymphony.xwork2.Action;
import com.opensymphony.xwork2.ActionContext;

/**
 * 
 * @author sunpeng
 *
 */
public class DesktopAction extends BaseAction {

	private static final long serialVersionUID = -862703582696888108L;
	private ModuleFactory moduleFactory;
	
	public String execute() throws Exception {
        
		ActionContext context = ServletActionContext.getContext();
		//暂时没有实现 用户的鉴权
		List<Module> modules=this.moduleFactory.getAuthorizedModules(null);
		context.put("modules", modules);
		context.put("constants", JSONObject.fromObject(this.getEnv()));
		return Action.SUCCESS;
	}
	
	
	protected Map<String,Object> getConstants()
	{
		Map<String,Object> map=new HashMap<String,Object>();
		map.put("userId", "1");
		map.put("deskUrl", super.getEnv().getProperty("deskUrl"));
		map.put("homeUrl", super.getEnv().getProperty("homeUrl"));
		map.put("deskModulesUrl", super.getEnv().getProperty("deskModulesUrl"));
		return map;
		
	}
	
	public void setModuleFactory(ModuleFactory moduleFactory) {
		this.moduleFactory = moduleFactory;
	}
	
	public static void main(String[] a)
	{
		String deskModulesUrl="http://localhost:8080/huarui/htdocs/desktop/modules";
		DesktopAction action=new DesktopAction();
		System.out.println(action.getAuthorizedModules(deskModulesUrl));
		
		Properties map=new Properties();
		map.put("deskUrl", "dd");
		map.put("deskUrl1", "dd");
		Object jsonObject = JSONObject.fromObject(map);
		System.out.println(jsonObject.toString());
	}
	protected List<Module> getAuthorizedModules(String deskModulesUrl)
	{
		String url=deskModulesUrl.endsWith("/")?deskModulesUrl:deskModulesUrl+"/";
		List<Module> modules=new ArrayList<Module>();
		Module bogusModule=new Module();
		bogusModule.setId("demo-bogus");
		bogusModule.setType("demo/bogus");
		bogusModule.setClassName("Uec3.BogusWindow");
		bogusModule.setScriptUrl(url+"bogus/bogus-win.js");
		bogusModule.setStylesheetUrl(url+"bogus/resources/styles.css");
		
		ModuleLauncher launcher=new ModuleLauncher();
		launcher.setIconCls("bogus-icon");
		launcher.setShortcutIconCls("demo-bogus-shortcut");
		launcher.setText("多重窗口演示");
		launcher.setTooltip("多重窗口演示");
		bogusModule.setLauncher(launcher);
		
		Map<ModuleLauncherAnchor, String> anchors=new HashMap<ModuleLauncherAnchor, String>();
		anchors.put(ModuleLauncherAnchor.startmenu, "/Desktop演示模块");
		bogusModule.setLauncherPaths(anchors);
		
		Map<String, String> initParams=new HashMap<String, String>();
		initParams.put("param1", "value1");
		bogusModule.setInitParams(initParams);
		
		bogusModule.setInQuickstart(true);
		bogusModule.setInShortcut(true);
		modules.add(bogusModule);

		Module accountModule=new Module();
		accountModule.setId("demo-account");
		accountModule.setType("demo/account");
		accountModule.setClassName("Uec3.AccountWindow");
		accountModule.setScriptUrl(url+"account/account.js");
		//accountModule.setStylesheetUrl(url+"bogus/resources/styles.css");
		
		ModuleLauncher accountlauncher=new ModuleLauncher();
		accountlauncher.setIconCls("acc-icon");
		accountlauncher.setShortcutIconCls("demo-acc-shortcut");
		accountlauncher.setText("我的帐户");
		accountlauncher.setTooltip("我的帐户");
		accountModule.setLauncher(accountlauncher);
		
		Map<ModuleLauncherAnchor, String> accountanchors=new HashMap<ModuleLauncherAnchor, String>();
		//accountanchors.put(ModuleLauncherAnchor.startmenutool, "/资料管理");
		accountModule.setLauncherPaths(accountanchors);
		
		Map<String, String> accouninitParams=new HashMap<String, String>();
		accouninitParams.put("userId", "1");
		accountModule.setInitParams(accouninitParams);
		
		accountModule.setInQuickstart(true);
		accountModule.setInShortcut(true);
		modules.add(accountModule);

		return modules;
	}
}
